package com.example.primomobile;

import java.util.HashMap;

import android.content.ClipData;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;

import com.example.primomobile.utils.Utils;

class PecasLongClickListener implements OnLongClickListener {

	@Override
	public boolean onLongClick( View view ) {
		if ( view != null ) {
			ClipData data = ClipData.newPlainText( "", "" );
			DragShadowBuilder shadowBuilder = new View.DragShadowBuilder( view );

			view.startDrag( data, shadowBuilder, getImagePecas( (ImageView) view ), 0 );

			imgDefaultDragPeca( ( (ImageView) view ) );
			view.setVisibility( View.VISIBLE );
			return true;
		} else {
			return false;
		}

	}

	private void imgDefaultDragPeca(ImageView view) {
		int position = (Integer) ((HashMap<String, Object>) view.getTag()).get(Utils.POSICAO);

		if (((position) / 4) % 2 == 0) {
			if ((position + 1) % 4 == 0) {
				view.setImageResource(R.drawable.img_default2);
			} else if ((position) % 4 == 0) {
				view.setImageResource(R.drawable.img_default6);
			} else {
				view.setImageResource(R.drawable.img_default1);
			}

		} else {
			if ((position + 1) % 4 == 0) {
				view.setImageResource(R.drawable.img_default3);
			} else if ((position) % 4 == 0) {
				view.setImageResource(R.drawable.img_default5);
			} else {
				view.setImageResource(R.drawable.img_default4);
			}
		}
	}

	private View getImagePecas(ImageView view) {
		HashMap<String, Object> hashMap = (HashMap<String, Object>) view.getTag();

		if (((String) hashMap.get(Utils.PECA)).equals(Utils.FRENTE)) {
			view.setImageResource(R.drawable.frente);
		} else if (((String) hashMap.get(Utils.PECA)).equals(Utils.ESQUERDA)) {
			view.setImageResource(R.drawable.esquerda);
		} else if (((String) hashMap.get(Utils.PECA)).equals(Utils.DIREITA)) {
			view.setImageResource(R.drawable.direita);
		} else if (((String) hashMap.get(Utils.PECA)).equals(Utils.FUNCAO)) {
			view.setImageResource(R.drawable.funcao);
		}

		return view;
	}
}